<?php
/**
 * 基础物料的队列任务导入最终执行Lib类
 *
 * @author kamisama.xia@gmail.com 
 * @version 0.1
 */

class material_basic_material_to_import {

    /**
     * 基础物料关联条码的队列任务执行
     * 
     * @param String $cursor_id
     * @param Array $params
     * @param String $errmsg
     * @return Boolean
     */
    function run(&$cursor_id,$params,&$errmsg){
        $importObj = app::get($params['app'])->model($params['mdl']);
        $barcodeObj = app::get('material')->model('barcode');
        $basicMaterialExtObj = app::get('material')->model('basic_material_ext');
        $basicMaterialFeatureGrpObj = app::get('material')->model('basic_material_feature_group');
        $basicMaterialStockObj = app::get('material')->model('basic_material_stock');
        $basicMaterialConfObj = app::get('material')->model('basic_material_conf');
        
        $basicMaterialConfObjSpe    = app::get('material')->model('basic_material_conf_special');
        
        $dataSdf               = $params['sdfdata'];
        $createSalesByBmIds    = array();//自动生成销售物料的基础物料bm_id

        foreach ($dataSdf as $v){
            $importData = array(
                'material_name' => $v['material_name'],
                'material_bn' => $v['material_bn'],
                'material_bn_crc32' => $v['material_bn_crc32'],
                'type' => $v['type'],
                'visibled' => $v['visibled'],
                'create_time' => time(),
                'omnichannel' => intval($v['omnichannel']),
            );
            
            $is_save = $importObj->save($importData);
            if($is_save){
                //保存条码信息
                $sdf = array(
                    'bm_id' => $importData['bm_id'],
                    'type' => material_codebase::getBarcodeType(),
                    'code' => $v['material_code'],
                );
                $barcodeObj->insert($sdf);

                //保存保质期配置
                $useExpireConfData = array(
                    'bm_id' => $importData['bm_id'],
                    'use_expire' => $v['use_expire'],
                    'warn_day' => $v['warn_day'] ?  $v['warn_day'] : 0,
                    'quit_day' => $v['quit_day'] ? $v['quit_day'] : 0,
                    'create_time' => time(),
                );
                $basicMaterialConfObj->save($useExpireConfData);

                //如果是组合物料保存相关数据
                if($v['type'] == 2){
                    $basicMaterialCombinationItemsObj = app::get('material')->model('basic_material_combination_items');
                    foreach($v['at'] as $k=>$num){
                        $tmpChildMaterialInfo = $importObj->dump($k, 'material_name,material_bn');

                        $addCombinationData = array(
                            'pbm_id' => $importData['bm_id'],
                            'bm_id' => $k,
                            'material_num' => $num,
                            'material_name' => $tmpChildMaterialInfo['material_name'],
                            'material_bn' => $tmpChildMaterialInfo['material_bn'],
                            'material_bn_crc32' => sprintf('%u',crc32($tmpChildMaterialInfo['material_bn'])),
                        );
                        $basicMaterialCombinationItemsObj->insert($addCombinationData);
                        $addCombinationData = null;
                    }
                }

                //保存基础物料的关联的特性
                //to do 暂时去掉这块逻辑，有待实现

                //保存物料扩展信息
                $addExtData = array(
                    'bm_id' => $importData['bm_id'],
                    'cost' => $v['cost'],
                    'retail_price' => $v['retail_price'],
                    'weight' => $v['weight'],
                    'unit' => $v['unit'],
                    'cat_id' => $v['cat_id'],
                    'specifications' => $v['specifications'],
                    'brand_id' => $v['brand_id'],
                );
                $basicMaterialExtObj->insert($addExtData);
                
                //保存物料库存信息
                $addStockData = array(
                    'bm_id' => $importData['bm_id'],
                    'store' => 0,
                    'store_freeze' => 0,
                );
                $basicMaterialStockObj->insert($addStockData);

                //保存特殊扫码配置信息
                $addScanConfInfo    = array(
                                        'bm_id' => $importData['bm_id'],
                                        'openscan' => $v['special_setting'],
                                        'fromposition' => $v['first_num'],
                                        'toposition' => $v['last_num'],
                                    );
                $basicMaterialConfObjSpe->insert($addScanConfInfo);
                
                //是否自动生成销售物料
                $v['create_material_sales']    = intval($v['create_material_sales']);
                if($v['create_material_sales'] === 1)
                {
                    $createSalesByBmIds[]    = $importData['bm_id'];
                }
            }else{
                $m = $importObj->db->errorinfo();
                if(!empty($m)){
                    $errmsg.=$m.";";
                }
            }
        }
        
        //自动生成销售物料
        if($createSalesByBmIds)
        {
            $bm_ids    = implode(',', $createSalesByBmIds);
            $result    = kernel::single('material_basic_exchange')->process($bm_ids);
            
            if($result['fail'] > 0)
            {
                $errmsg    .= "自动生成销售物料成功:". $result['total'] ."个,失败:". $result['fail'] ."个";
            }
        }
        
        return false;
    }
}
